package view;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseWheelListener;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

import model.Image;
import model.Perspective;

@SuppressWarnings("serial")
public class ImagePanel extends JPanel implements IImagePanel, IView {
	
	private Perspective perspective;
	private Image image;
	
	public ImagePanel(Perspective perspective, Image image) {
		this.perspective = perspective;
		this.image = image;
		
		perspective.registerObserver(this);
		image.registerObserver(this);
	}
	
	@Override
	public void update() {
		repaint();
	}
	
	@Override
	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		Graphics2D graphics2D = (Graphics2D)graphics;
		if(image.isNew())
			perspective.reset();
		if(image.isEnable()) {
		graphics2D.drawImage(image.getScaledImage(perspective.getZoomPercentage()),
												  perspective.getxPosition(),
												  perspective.getyPosition(), this);
		}
		else
			perspective.reset();
	}
	
	public void addTranslationListener(MouseInputAdapter listener) {
		addMouseListener(listener);
		addMouseMotionListener(listener);
	}
	
	public void addZoomListener(MouseWheelListener listener) {
		addMouseWheelListener(listener);
	}
	
}